/** ***********************************************************************
*  $Id: $
*
*  COPYRIGHT @2008 CSS CORP PVT LTD
*          
*  This program is free software; you can redistribute it and/or modify it under the terms of 
*  version 2 of the GNU General Public License as published by the Free Software 
*  Foundation.
*
*  The above copyright notice and this permission notice shall be included in all copies 
*  or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
*  DEALINGS IN THE SOFTWARE.
*
************************************************************************* */

package com.css.ctg.tulip.broker;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

import com.css.ctg.tulip.exception.TulipException;
import com.css.ctg.tulip.jaxb.config.TulipConfig;
import com.css.ctg.tulip.util.TulipLogger;


/**
 * <P> Framework factory class. Factory object to create instance of framework broker.
 * 		Used to bind the tulip-config.xml to java object. 
 * 		Create a instance for the TulipBroker with the tulipConfig object parameter.
 * 
 * @version $Rev:
 * 			$
 *
 */

public class TulipFactory {
	
	private String className = "TulipFactory";
		
	private TulipConfig tulipConfig=null;	
	
	/**
	 * <p>	This constructor will get the config.xml path as input and bind the xml to the java object.
	 * 
	 * @param inputStream the path of config.xml file
	 * @throws TulipException
	 */
	public TulipFactory(InputStream inputStream) throws TulipException {
		// Logging action
		TulipLogger.log(className, TulipLogger.info, "inside TulipFactory(InputStream inputStream) of TulipFactory");
	
		
		//calls the jaxbUnmarshalling(inputStream) to unmarshal the xml.
		this.tulipConfig=jaxbUnmarshalling(inputStream);		
	}
	
	/**
	 * <p>	Used to convert XML files to java object.
	 * 
	 * @param inputStream - InputStream	 * 
	 * @return TulipConfig
	 * @throws TulipException
	 */

	public TulipConfig jaxbUnmarshalling(InputStream inputStream) throws TulipException {
		
		// Logging action
		TulipLogger.log(className, TulipLogger.info, "inside jaxbUnmarshalling(InputStream inputStream) of TulipFactory");

		JAXBContext jc = null;
		Unmarshaller unmarshaller = null;
		
		try{				
			//For UnMarshalling the XML file.
			jc = JAXBContext.newInstance("com.css.ctg.tulip.jaxb.config");
			unmarshaller = jc.createUnmarshaller();
			
			//Validating the xml file with the schema.Throws an error if the xml doesn't match the schema.
			SchemaFactory schemaFactory=SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
			
			javax.xml.validation.Schema schema=schemaFactory.newSchema(new StreamSource(TulipFactory.class.getResourceAsStream("tulip-config.xsd")));
			
			unmarshaller.setSchema(schema);
			
			//unmarshall the xml file specified in inputstream. tulipconfig holds the xml content.			
			return (TulipConfig)unmarshaller.unmarshal(inputStream);
		} catch(SAXException saxe){
			throw new TulipException(className, saxe);
		} catch (JAXBException jaxbe) {
			throw new TulipException(className, jaxbe);
		} catch (Exception e) {
			throw new TulipException(className, e);	
		}
	}

	/**
	 * Create instance of TulipBroker.
	 * 
	 * @return  TulipBroker
	 */
	public TulipBroker getBroker() {	
		// Logging action
		TulipLogger.log(className, TulipLogger.info, "inside getInstance() of TulipFactory");

		return new TulipBroker(tulipConfig);
	}		
}
